package com.hong.qrpc.spring.springboot.starter;

import com.hong.qrpc.RpcClient;
import com.hong.qrpc.spring.springboot.starter.config.QrpcAutoConfiguration;
import com.hong.qrpc.spring.springboot.starter.postprocessor.QrpcPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author HQT
 * @Date 2022/8/1 11:23
 */
//不使用组件扫描器就无法使得postProcess被正确的触发回调事件
@ComponentScan(basePackages = {"com.hong.qrpc.spring.springboot.starter"})
@Component
public class ClientStarter {
    public void clientStart(QrpcPostProcessor postProcessor) throws IOException {
        ConfigurableListableBeanFactory beanFactory = postProcessor.getBeanFactory();
        RpcClient rpcClient = new RpcClient(postProcessor.getRemoteInvokes(),
                beanFactory.getBean(QrpcAutoConfiguration.class).getProfile(), beanFactory);
        beanFactory.registerSingleton("rpcClient", rpcClient);
    }
}
